//
//  HomeViewController.swift
//
//  Created by JohnLui on 15/4/10.
//  Copyright (c) 2015年 com.lvwenhan. All rights reserved.
//

import UIKit
import Photos

// 主页
class HomeViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UITableViewDelegate, UITableViewDataSource {
    
    @IBOutlet weak var FromCityBtn: UIButton!
    @IBOutlet weak var ToCityBtn: UIButton!
    
    //到达城市点击事件
    @IBAction func toCityBtnClick(sender: AnyObject) {
        let viewController = Common.rootViewController
        viewController.homeViewController.performSegueWithIdentifier("showCityInfoTo", sender: self)
        viewController.mainTabBarController.tabBar.hidden = true
    }
    
    //出发城市点击事件
    @IBAction func fromCityBtnClick(sender: AnyObject) {
        let viewController = Common.rootViewController
        viewController.homeViewController.performSegueWithIdentifier("showCityInfoFrom", sender: self)
        viewController.mainTabBarController.tabBar.hidden = true
    }
    
    
    
    var titleOfOtherPages = ""

    @IBOutlet weak var PersonalPic: UIImageView!
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet var panGesture: UIPanGestureRecognizer!

    //数据源
    var dataSource:[AircrewMember] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // 设置中间 segmentView 视图
        let segmentView = UISegmentedControl(items: ["消息", "电话"])
        segmentView.selectedSegmentIndex = 0
        segmentView.setWidth(60, forSegmentAtIndex: 0)
        segmentView.setWidth(60, forSegmentAtIndex: 1)
        //self.navigationItem.titleView = segmentView
        
        PersonalPic.image = UIImage(named:  "机长")
        
        self.tableView!.delegate = self
        self.tableView!.dataSource = self
        
        self.collectionView!.delegate = self
        self.collectionView!.dataSource = self
 
        //创建一个重用的单元格
        self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "viewCell")
        self.view.addSubview(self.tableView!)
        
        
    }
    
    override func viewDidAppear(animated: Bool) {
        Common.rootViewController.mainTabBarController.tabBar.hidden = false

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //MARK: -tableview
    //每个区域的cell数
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch section {
        //机组
        case 0:
            return 4
        //常用操作
        case 1:
            return 4
        //注意事项
        case 2:
            return 3
            
        default:
            return 0
        }
        return 5
    }
    
    //区域数
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    
    //每个section的标题
    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        switch section {
    case 0:
        return "公告信息"
        
    default:
        return "注意事项"
        }
    }
    
    //点击群聊
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//        //机组成员区域
//        if(indexPath.section == 0){
//            let viewController = Common.rootViewController
//            viewController.homeViewController.performSegueWithIdentifier("showUUChatView", sender: self)
//            viewController.mainTabBarController.tabBar.hidden = true
//
//            
//        }
//        //常用操作区域
//        if(indexPath.section == 1){
//            let viewController = Common.rootViewController
//            viewController.homeViewController.performSegueWithIdentifier("showUUChatView", sender: self)
//        }
    }
    
    //cell填充
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        //let cell = tableView.dequeueReusableCellWithIdentifier("viewCell", forIndexPath: indexPath)
        
        let cell:UITableViewCell = UITableViewCell(style:UITableViewCellStyle.Subtitle, reuseIdentifier: "viewCell")
        
        let index = indexPath.row
        let sectionNum = indexPath.section
        var role:String = "注意事项"
        

        //常用操作区域
        if(sectionNum == 1){
            
        }
        
        let image = UIImage(named: role)
        //背景图片
        cell.imageView?.image = image
        //cell.backgroundColor = UIColor.blackColor()
        //cell.textLabel!.text = titlesDictionary[indexPath.row]
        cell.textLabel!.text = "本次航班有在押犯人"
        cell.detailTextLabel!.text = role
            
        return cell
    }
    
    
    //MARK: -Collectionview
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let CellIdentifier = "Cell"
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(CellIdentifier, forIndexPath: indexPath) as! CollectionViewCell
        
        // 获取所有资源的集合，并按资源的创建时间排序
        var options = PHFetchOptions()
        options.sortDescriptors?.append( NSSortDescriptor(key:"creationDate", ascending: true) )
        
        var assetsFetchResults = PHAsset.fetchAssetsWithOptions(options)
        
        
        var i = indexPath.row%5
        
            var role:String = "空乘"
            if(indexPath.row == 0){
                role = "机长"
            }
            if(indexPath.row  == 1){
                role = "副驾"
            }
            
            if(indexPath.row  == 2){
                role = "乘务长"
            }
            if(indexPath.row  == 3){
                role = "安全员"
            }
            if(indexPath.row  == 4){
                role = "空乘"
            }
            cell.headlabel.text = role
            cell.headimage.image = UIImage(named: role)
        return cell
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        let viewController = Common.rootViewController
        viewController.homeViewController.performSegueWithIdentifier("showUUChatView", sender: self)
        viewController.mainTabBarController.tabBar.hidden = true

    }
    
//    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
//        return 1
//    }
    
    
    
    
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "showOtherPages" {
            if let a = segue.destinationViewController as? OtherPageViewController {
                a.PageTitle = titleOfOtherPages
            }
        }
        
        //出发城市
        if segue.identifier == "showCityInfoFrom"{
            if let a = segue.destinationViewController as? CityInfoViewController {
                a.cityName = (FromCityBtn.titleLabel?.text)!
            }
        }
        //到达城市
        if segue.identifier == "showCityInfoTo"{
            if let a = segue.destinationViewController as? CityInfoViewController {
                a.cityName = (ToCityBtn.titleLabel?.text)!
            }
        }
        //群聊
        if segue.identifier == "showUUChatView"{
            if let a = segue.destinationViewController as? UUChatViewController {
                //a.cityName = (ToCityBtn.titleLabel?.text)!
            }
        }
    }
}
